#include "taggedfile.h"

#include <QDebug>
#include <QImage>

void TaggedFile::file(const QString fileName, Collection & collection)
{
    /* Generic tag (isn't depend of the type of the file) */
    TagLib::FileRef file(fileName.toStdString().c_str());
    TagLib::Tag *genericTag = file.tag();
    if(genericTag && !genericTag->isEmpty())
    {
        TagLib::String artist = genericTag->artist();
        TagLib::String genre = genericTag->genre();
        TagLib::String album = genericTag->album();
        int year = genericTag->year();
        TagLib::String title = genericTag->title();
        int track = genericTag->track();
        /* Duration */
        QTime duration;
        if(file.audioProperties())
        {
            TagLib::AudioProperties *properties = file.audioProperties();
            duration = duration.addSecs(properties->length());
        }

        DtAlbum *dAlbum = new DtAlbum(album.toCString(true), year);
        DtArtist *art;
        DtAlbum *alb;
        DtGenre *gen;
        if(collection.addAlbum(dAlbum))
        {
            alb = dAlbum;
        } else {
            alb = collection.getAlbum(dAlbum);
            delete dAlbum;
        }


        DtArtist *dArtist = new DtArtist(artist.toCString());
        if(collection.addArtist(dArtist))
        {
            art = dArtist;
        } else {
            art = collection.getArtist(dArtist);
            delete dArtist;
        }

        DtGenre *dGenre = new DtGenre(genre.toCString());
        if(collection.addGenre(dGenre))
        {
            gen = dGenre;
        } else {
            gen = collection.getGenre(dGenre);
            delete dGenre;
        }

        DtTrack *dTrack = new DtTrack(fileName, title.toCString(), track, -1,
                                      alb, art, gen, duration, QString::null);
        collection.addTrack(dTrack, alb, art, gen);
        /* Isn't generic : number of tracks / cover art / lyrics
     * That depend of the type of the file. */
        QString fileType = fileName.right(fileName.lastIndexOf(".")).toUpper();
        //    TagLib::ByteVector imageData = imageFile.data();
        if (fileType == "MP3")
        {
            TagLib::MPEG::File audioFile(fileName.toStdString().c_str());

            TagLib::ID3v2::FrameList frameList = audioFile.ID3v2Tag()->
                    frameListMap()["APIC"];
            if(!frameList.isEmpty())
            {
                TagLib::ID3v2::AttachedPictureFrame *cover =
                        static_cast<TagLib::ID3v2::AttachedPictureFrame *>
                        (frameList.front());
                QImage img;
                img.loadFromData((const uchar *)cover->picture().data(),
                                 (int)cover->picture().size());
            }
            /*
        if(img.save("/home/zephyrin/coverTest.jpg"))
            qDebug() << "Save image";
        else
            qDebug() << "Image cannot save";
            */
        }
        else if (fileType == "OGG" || fileType == "FLAC")
        {
        //        TagLib::Ogg::Vorbis::File audioFile(argv[1]);
        //        TagLib::Ogg::XiphComment *tag = audioFile.tag();
        //        TagLib::FLAC::Picture* picture = new TagLib::FLAC::Picture();
        //        picture->setData(imageData);
        //        picture->setType((TagLib::FLAC::Picture::Type) 0x03); // FrontCover
        //        picture->setMimeType("image/jpeg");
        //        picture->setDescription("Front Cover");
        //        TagLib::ByteVector block = picture->render();
        //        tag->addField("METADATA_BLOCK_PICTURE", b64_encode(block.data(),
        //                                                           block.size()), true);
        //        block = imageData;
        //        tag->addField("COVERART", b64_encode(block.data(), block.size()), true);
        //        audioFile.save();
        }
        else
        {
         //   qWarning() << fileType << " is unsupported.";
        }
    }
    else
    {
        collection.addTrack(new DtTrack(fileName), NULL, NULL, NULL);
    }
}
